package com.gdufe.studytogether.task;

import com.alibaba.druid.util.StringUtils;
import com.gdufe.studytogether.domain.pojo.Comment;
import com.gdufe.studytogether.service.comment.CommentService;
import com.gdufe.studytogether.utils.DirtyWorldFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@ConditionalOnProperty(name = "autoDFS", matchIfMissing = true)
public class ScheduledTask {
    
    @Autowired(required = false)
    RedisTemplate redisTemplate;
    @Autowired
    CommentService commentService;
    
    @Scheduled(cron ="*/5 * * * * ?")
    public void exec(){
        ListOperations listOperations = redisTemplate.opsForList();
        String str = (String)listOperations.rightPop("comments");

        if (StringUtils.isEmpty(str)){
            return ;
        }else{
            String[] strings = str.split("/@/#/@/");
            String commentId = strings[0];
            String content = strings[1];
            if (StringUtils.isEmpty(commentId)){
                return;
            }
            Comment comment = commentService.getById(Integer.valueOf(commentId));
            if (comment==null){
                return;
            }
            System.out.println("敏感词过滤:"+commentId);
            System.out.println("原标题:"+comment.getTitle());
            String newTitle="**********";
            try{
                newTitle = DirtyWorldFilter.doFilter(comment.getTitle());
            }catch (Exception e){
                System.out.println("敏感词过滤失败:"+e.getMessage());
                e.printStackTrace();
            }
            System.out.println("标题过滤完成:"+newTitle);
            System.out.println("原文:"+content);
            String newContent="**********";
            try{
                newContent = DirtyWorldFilter.doFilter(content);
            }catch (Exception e){
                System.out.println("敏感词过滤失败:"+e.getMessage());
                e.printStackTrace();
            }
            System.out.println("原文过滤完成:"+newContent);
            comment.setTitle(newTitle);
            comment.setContent(newContent);
            comment.setCmStatus(1);
            boolean b = commentService.updateById(comment);


        }
    }
}
